<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/15
 * Time: 15:03
 */

namespace Controller;

class MemberController extends TooController{
    //个人中心
    public function index(){
        $this->view->display();
    }

    public function orderlist(){
        //显示我的所有订单数据
        $order=$this->pdo->pdo_sel("select * from s_order where m_id = '{$_SESSION['Home']['Member']['id']}'",2);

        $this->view->assign('order',$order);
        $this->view->display();
    }

    //修改订单方法
    public function order_edit(){
        $id=$_GET['id'];
        $type=$_GET['type'];

        if($id&&$type){
            if($this->pdo->pdo_upd(array('start'=>$type),'s_order', " id = $id")){
                $this->success('修改成功','Member','orderlist');
            }
            else{
                $this->success('修改失败','Member','orderlist');
            }
        }
        $this->success('非法操作','Member','orderlist');
    }

    //查看订单
    public function order_msg(){
        $id=$_GET['id'];
        if(empty($id)){
            $this->success('非法操作','Member','orderlist');
        }

        //订单详情
        $order=$this->pdo->pdo_sel("select * from s_order where id = '{$id}'");

        //订单商品详情
        $goods=$this->pdo->pdo_sel("select o.*,g.img,g.name,gg.gname from s_order_s as o join s_goods as g on o.g_id=g.id join s_goods_g as gg on o.gg_id = gg.id where o.o_id = {$order['id']}",'2');
        $this->view->assign('order',$order);
        $this->view->assign('goods',$goods);
        $this->view->display();
    }

    //我的账号
    public function user(){
        if($_POST){
            $arr=array();
            $arr['pass']=$_POST['pass'];
            if($_FILES){
             $arr['img']=$this->imgFile($_FILES['img'],'/Public/Home/user/');
            }
            $this->pdo->pdo_upd($arr,'s_member'," id = '{$_SESSION['Home']['Member']['id']}'");

            //修改session
            $_SESSION['Home']['Member']['pass']=$arr['pass'];
            $_SESSION['Home']['Member']['img']=$arr['img'];
            $this->success('修改成功');
        }
        $this->view->display();
    }

    //充值操作
    public function money(){
        if($_POST){
            $arr=array();
            $arr['money']=$_POST['money'];
            $arr['time']=date("Y-m-d H:i:s",time());
            $arr['ordersn']=date("YmdHis",time()).rand(10,99);
            $arr['m_id']=$_SESSION['Home']['Member']['id'];
            $arr['start']='1';
            if($this->pdo->pdo_ins($arr,"s_money")){
                //调用支付宝支付
                $this->zfbPay($arr['ordersn'],'会员充值',$arr['money']);
            }else{
                $this->success('系统错误');
            }
        }else if($_GET['id']){
            $id=$_GET['id'];
            $arr=$this->pdo->pdo_sel("select * from s_money where id = $id");
            //调用支付宝支付
            $this->zfbPay($arr['ordersn'],'会员充值',$arr['money']);
        }
        $this->view->display();
    }

    //支付成功同步通知
    public function moneyY(){
        //调用验证方法
        if($this->zfbPay_q()){
            //修改订单状态并且添加余额
            $ordersn=$_GET['trade_no'];
            $this->pdo->pdo_upd(array('start'=>2),'s_money'," ordersn='$ordersn'");

            //添加余额
            $money=$_SESSION['Home']['Member']['money']+$_GET['total_fee'];
            $this->pdo->pdo_upd(array("money"=>$money),"s_member"," id = {$_SESSION['Home']['Member']['id']}");
            $_SESSION['Home']['Member']['money']=$money;

            $this->success('充值成功','Member','moneyL');
        }
    }
    //充值记录
    public function moneyL(){
        $arr=$this->pdo->pdo_sel("select * from s_money where m_id = '{$_SESSION['Home']['Member']['id']}'",2);
        $this->view->assign('arr',$arr);
        $this->view->display();
    }
}